package tripx.plan;

import java.util.HashMap;

import tripx.server.Service;
import tripx.sql.SqlHandler;
import tripx.sql.UpdateSqlHandler;
import tripx.system.Logger;

/**
 * The standard interfaces for the request for plan service. These methods are
 * all for basic operation.
 * 
 * @author zhb
 * 
 */
public class PlanService extends Service {

	/**
	 * configuration
	 */
	PlanServiceConfig config;
	/**
	 * handle the table plan
	 */
	private UpdateSqlHandler planHandler;
	/**
	 * handle the table plancity
	 */
	private UpdateSqlHandler planCityHandler;

	public PlanService() {
		config = PlanServiceConfig.getInstance();
		// initialize these handlers
		planHandler = new UpdateSqlHandler(config.getPlanTableName());
		planCityHandler = new UpdateSqlHandler(config.getPlanCityTableName());

		sqlHandler = new SqlHandler(config.getQueryActionList());

		logger = Logger.getInstance();
	}

	/**
	 * add a new Plan to the database
	 * 
	 * @param plan
	 * @return
	 */
	public boolean addPlan(DbPlan plan) {
		logger.info("Add Plan Begins...");
		String sql = planHandler.getSqlAdd(plan);
		logger.info("\tSend Sql: " + sql);
		int status = invoker.executeUpdateSql(sql) & planHandler.getStatus();
		logger.info("\tStatus: " + status);
		logger.info("Add Plan Ends...");
		if (status == 0) {
			return false;
		}
		return true;
	}

	/**
	 * add the detailed information into the database, the parameter tells the
	 * plan will cover which cities and also the time.
	 * 
	 * @param city
	 * @return
	 */
	public boolean addPlanCity(DbPlanCity city) {
		String sql = planCityHandler.getSqlAdd(city);
		int status = invoker.executeUpdateSql(sql)
				& planCityHandler.getStatus();
		if (status == 0) {
			return false;
		}
		return true;
	}

	/**
	 * delete the plan with only one key-value pair, this is for convenience
	 * when delete the plan by id.
	 * 
	 * @param key
	 *            specify which column
	 * @param value
	 *            the value on that column
	 * @return
	 */
	public boolean deletePlan(String key, String value) {
		String sql = planHandler.getSqlDelete(key, value);
		int status = planHandler.getStatus() & invoker.executeUpdateSql(sql);
		if (status == 0) {
			return false;
		}
		return true;
	}

	/**
	 * delete an existed plan
	 * 
	 * @param kvMap
	 * @return
	 */
	public boolean deletePlan(HashMap<String, String> kvMap) {
		String sql = planHandler.getSqlDelete(kvMap);
		int status = planHandler.getStatus() & invoker.executeUpdateSql(sql);
		if (status == 0) {
			return false;
		}
		return true;
	}

	/**
	 * delete the plan-city item in the plancity table
	 * 
	 * @param kvMap
	 * @return
	 */
	public boolean deletePlanCity(HashMap<String, String> kvMap) {
		String sql = planCityHandler.getSqlDelete(kvMap);
		int status = planCityHandler.getStatus()
				& invoker.executeUpdateSql(sql);
		if (status == 0) {
			return false;
		}
		return true;
	}

	/**
	 * delete items in the plancity table
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean deletePlanCity(String key, String value) {
		String sql = planCityHandler.getSqlDelete(key, value);
		int status = planCityHandler.getStatus()
				& invoker.executeUpdateSql(sql);
		if (status == 0) {
			return false;
		}
		return true;
	}

}
